package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProduceOrderSaleRpcService;
import com.hunttown.mes.rpc.domain.MesProduceOrderSaleDTO;
import com.hunttown.mes.rpc.domain.query.MesProduceOrderSaleDTOQuery;
import com.hunttown.mes.service.MesProduceOrderSaleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProduceOrderSaleConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:29:32
 */
@Service
public class MesProduceOrderSaleRpcServiceImpl implements MesProduceOrderSaleRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProduceOrderSaleRpcServiceImpl.class);

    private final MesProduceOrderSaleService mesProduceOrderSaleService;

    @Autowired
    public MesProduceOrderSaleRpcServiceImpl(MesProduceOrderSaleService mesProduceOrderSaleService) {
        this.mesProduceOrderSaleService = mesProduceOrderSaleService;
    }

    @Override
    public MesProduceOrderSaleDTO insert(MesProduceOrderSaleDTO mesProduceOrderSaleDTO) {
        try {
            return convert(mesProduceOrderSaleService.insert(convert(mesProduceOrderSaleDTO)));
        } catch (Exception e) {
            logger.error("MesProduceOrderSale create failed.", e);
            throw new RuntimeException("MesProduceOrderSale create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProduceOrderSaleDTO mesProduceOrderSaleDTO) {
        try {
            return mesProduceOrderSaleService.updateInfoById(convert(mesProduceOrderSaleDTO));
        } catch (Exception e) {
            logger.error("MesProduceOrderSale update by id failed.", e);
            throw new RuntimeException("MesProduceOrderSale update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProduceOrderSaleDTOQuery mesProduceOrderSaleDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceOrderSaleDTOQuery.getW_id() != null && mesProduceOrderSaleDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProduceOrderSaleDTOQuery.getW_id());
            }

            if (mesProduceOrderSaleDTOQuery.getW_produceId() != null) {
                query.put("w_produceId", mesProduceOrderSaleDTOQuery.getW_produceId());
            }

            if (mesProduceOrderSaleDTOQuery.getIds() != null && !mesProduceOrderSaleDTOQuery.getIds().equals("")) {
                query.put("ids", mesProduceOrderSaleDTOQuery.getIds());
            }

            if (mesProduceOrderSaleDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceOrderSaleDTOQuery.getStopFlag());
            }

            if (mesProduceOrderSaleDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceOrderSaleDTOQuery.getDeleteFlag());
            }

            return mesProduceOrderSaleService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceOrderSale update by query failed.", e);
            throw new RuntimeException("MesProduceOrderSale update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceOrderSaleDTO selectById(Integer id) {
        try {
            return convert(mesProduceOrderSaleService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProduceOrderSale select by id failed.", e);
            throw new RuntimeException("MesProduceOrderSale select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceOrderSaleDTO selectObjByQuery(MesProduceOrderSaleDTOQuery mesProduceOrderSaleDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceOrderSaleDTOQuery.getProduceId() != null) {
                query.put("produceId", mesProduceOrderSaleDTOQuery.getProduceId());
            }

            if (mesProduceOrderSaleDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceOrderSaleDTOQuery.getStopFlag());
            }

            if (mesProduceOrderSaleDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceOrderSaleDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProduceOrderSaleDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProduceOrderSaleDTOQuery.getOrderBy());
            }

            return convert(mesProduceOrderSaleService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProduceOrderSale select obj by query failed.", e);
            throw new RuntimeException("MesProduceOrderSale select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProduceOrderSaleDTO> getListForPage(MesProduceOrderSaleDTOQuery mesProduceOrderSaleDTOQuery) {
        Query query = Query.page(mesProduceOrderSaleDTOQuery.getPn(), mesProduceOrderSaleDTOQuery.getPageSize());

        if (mesProduceOrderSaleDTOQuery.getId() != null && mesProduceOrderSaleDTOQuery.getId() > 0) {
            query.put("id", mesProduceOrderSaleDTOQuery.getId());
        }

        if (mesProduceOrderSaleDTOQuery.getIds() != null && !mesProduceOrderSaleDTOQuery.getIds().equals("")) {
            query.put("ids", mesProduceOrderSaleDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProduceOrderSaleDTOQuery.getLikeName())) {
            query.put("likeName", mesProduceOrderSaleDTOQuery.getLikeName());
        }

        if (mesProduceOrderSaleDTOQuery.getProduceId() != null) {
            query.put("produceId", mesProduceOrderSaleDTOQuery.getProduceId());
        }

        if (mesProduceOrderSaleDTOQuery.getSaleId() != null) {
            query.put("saleId", mesProduceOrderSaleDTOQuery.getSaleId());
        }

        if (mesProduceOrderSaleDTOQuery.getSaleDetailId() != null) {
            query.put("saleDetailId", mesProduceOrderSaleDTOQuery.getSaleDetailId());
        }

        if (StringUtils.isNotBlank(mesProduceOrderSaleDTOQuery.getShowDate())) {
            query.put("showDate", mesProduceOrderSaleDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProduceOrderSaleDTOQuery.getStartDate())) {
            query.put("startDate", mesProduceOrderSaleDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProduceOrderSaleDTOQuery.getEndDate())) {
            query.put("endDate", mesProduceOrderSaleDTOQuery.getEndDate());
        }

        if (mesProduceOrderSaleDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProduceOrderSaleDTOQuery.getStopFlag());
        }

        if (mesProduceOrderSaleDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProduceOrderSaleDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProduceOrderSaleDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProduceOrderSaleDTOQuery.getOrderBy());
        }

        List<MesProduceOrderSaleDTO> list = convert(mesProduceOrderSaleService.selectByQuery(query));
        Page<MesProduceOrderSaleDTO> page = Page.from(mesProduceOrderSaleDTOQuery.getPn(), mesProduceOrderSaleDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProduceOrderSaleService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProduceOrderSaleService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProduceOrderSale delete failed.", e);
            throw new RuntimeException("MesProduceOrderSale delete failed, Please order your code.", e);
        }
    }
}